<%
if (pageData.template === 'team') {
    pageData.config = {toc: false, sidebar: false};
}

// 构建静态资源路径函数（只考虑版本）
function buildAssetPath(assetPath) {
    let path = '';
    if (pageData.version) {
        path += '/' + pageData.version;
    }
    return path + (assetPath.startsWith('/') ? assetPath : '/' + assetPath);
}

// 构建页面路径函数（考虑版本和国际化）
function buildPagePath(pagePath) {
    let path = '';
    if (pageData.version) {
        path += '/' + pageData.version;
    }
    if (locals.siteData.feature?.i18n?.enable && pageData.language) {
        path += '/' + pageData.language;
    }
    return path + (pagePath.startsWith('/') ? pagePath : '/' + pagePath);
}
%>

<!DOCTYPE html>
<html lang="<%= pageData.language || 'zh' %>" class="scroll-smooth">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= pageData.title %> - <%= siteData.site.title %></title>
    <%- include('../includes/seo.ejs', { pageData, siteData }) %>

    <link rel="icon" href="<%= buildAssetPath(siteData.site.favicon || '/favicon.ico') %>" type="image/x-icon">

    <!-- 基础样式文件 -->
    <link rel="stylesheet" href="<%= buildAssetPath('/assets/pageforge.min.css') %>">

    <script>
        (function () {
            document.addEventListener('click', function (e) {
                var link = e.target.closest('a');
                if (!link) {
                    return;
                }

                const href = link.getAttribute('href');
                if (!href || href.startsWith('http') || href.startsWith('#')) {
                    return;
                }

                e.preventDefault();

                // 获取当前路径信息
                const pathParts = window.location.pathname.split('/').filter(Boolean);
                let version = null;
                let lang = null;

                // 解析当前 URL 中的版本和语言信息
                if (pathParts.length > 0) {
                    if (/^\d+\.\d+(\.\d+)?$/.test(pathParts[0]) || pathParts[0] === 'current') {
                        version = pathParts[0];
                        lang = pathParts[1];
                    }
                    else {
                        lang = pathParts[0];
                    }
                }

                // 构建 404 页面路径
                let notFoundPath = '';
                if (version) {
                    notFoundPath += '/' + version;
                }
                if (lang) {
                    notFoundPath += '/' + lang;
                }
                notFoundPath += '/404.html';

                fetch(href)
                    .then(response => {
                        if (response.ok) {
                            window.location.href = href;
                        }
                        else {
                            window.location.href = notFoundPath;
                        }
                    })
                    .catch(() => {
                        window.location.href = notFoundPath;
                    });
            });
        })();
    </script>

    <!-- 加载主题配置中的额外 CSS 文件 -->
    <% if (locals.siteData?.theme?.extraCss && Array.isArray(locals.siteData?.theme?.extraCss)) { %>
        <% locals.siteData?.theme?.extraCss.forEach(css_path => { %>
            <link rel="stylesheet" href="<%= buildAssetPath(css_path) %>">
        <% }); %>
    <% } %>

    <!-- Tailwind CSS -->
    <script src="<%= locals.siteData.cdn?.style || 'https://cdn.tailwindcss.com' %>"></script>

    <!-- Prism.js 语法高亮样式 -->
    <link href="<%= locals.siteData.cdn?.prismStyle || 'https://cdnjs.cloudflare.com/ajax/libs/prism-themes/1.9.0/prism-ghcolors.css' %>" rel="stylesheet"/>

    <!-- 统计代码 -->
    <% if (locals.siteData.feature?.statistics?.enable) { %>
        <%- locals.siteData.feature.statistics.content %>
    <% } %>

    <!-- Mermaid 图表 -->
    <% if (locals.siteData.feature?.mermaid?.enable) { %>
        <script src="<%= locals.siteData.cdn?.mermaidJs || 'https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js' %>"></script>
    <% } %>

    <!-- KaTeX 数学公式 -->
    <% if (locals.siteData.feature?.katex?.enable) { %>
        <link rel="stylesheet" href="<%= locals.siteData.cdn?.katexCss || 'https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css' %>">
        <script src="<%= locals.siteData.cdn?.katexJs || 'https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js' %>"></script>
    <% } %>
</head>

<body class="min-h-screen bg-white dark:bg-gray-900 text-gray-900 dark:text-gray-100">
<div class="flex flex-col min-h-screen">
    <% if (locals.pageData?.config?.header !== false) { %>
        <%- include('../includes/header') %>
    <% } %>

    <!-- 移动端菜单按钮 -->
    <% if (locals.pageData?.config?.sidebar !== false) { %>
        <button class="md:hidden fixed left-4 bottom-4 z-20 bg-white dark:bg-gray-800 p-2 rounded-full shadow-lg"
                onclick="document.getElementById('sidebar-menu').classList.remove('-translate-x-full');
                         document.getElementById('sidebar-overlay').classList.remove('hidden');">
            <svg class="w-6 h-6 text-gray-600 dark:text-gray-300" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
            </svg>
        </button>
    <% } %>

    <div class="pt-[--header-height] w-full">
        <div class="container mx-auto px-4 py-4 md:py-8 flex-grow flex flex-col md:flex-row">
            <!-- 左侧列表 -->
            <% if (locals.pageData?.config?.sidebar !== false) { %>
                <aside class="fixed md:relative md:w-56 lg:w-64 flex-shrink-0 md:pr-4 lg:pr-8
                         inset-y-0 left-0 transform -translate-x-full md:translate-x-0
                         transition duration-200 ease-in-out
                         md:block z-30 bg-white dark:bg-gray-900 md:bg-transparent
                         w-3/4 sm:w-64 top-[--header-height] md:top-auto"
                       id="sidebar-menu">
                    <div class="h-16 md:hidden flex items-center justify-end px-4 border-b dark:border-gray-800">
                        <button class="text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100"
                                onclick="document.getElementById('sidebar-menu').classList.add('-translate-x-full');
                                     document.getElementById('sidebar-overlay').classList.add('hidden');">
                            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                            </svg>
                        </button>
                    </div>
                    <nav class="md:sticky p-4 md:p-0">
                        <div class="space-y-4">
                            <%- include('../includes/sidebar') %>
                        </div>
                    </nav>
                </aside>
            <% } %>

            <!-- 中间内容 -->
            <main class="flex-grow w-full md:w-2">
                <article class="prose dark:prose-invert lg:prose-lg mx-auto
                          max-w-none md:max-w-2xl lg:max-w-full">
                    <%- include(`${pageData.template || 'content'}`) %>
                </article>
            </main>

            <!-- 右侧目录 -->
            <% if (locals.pageData?.config?.toc !== false) { %>
                <%- include('../includes/toc') %>
            <% } %>
        </div>
    </div>

    <% if (locals.pageData?.config?.footer !== false) { %>
        <%- include('../includes/footer') %>
    <% } %>
</div>

<!-- 返回顶部按钮 -->
<%- include('../includes/feature-backTop') %>

<!-- 遮罩层 -->
<% if (locals.pageData?.config?.sidebar !== false) { %>
    <div class="md:hidden fixed inset-0 bg-gray-900 bg-opacity-50 z-20 hidden"
         id="sidebar-overlay"
         onclick="document.getElementById('sidebar-menu').classList.add('-translate-x-full');
                  this.classList.add('hidden');">
    </div>
<% } %>
</body>

<!-- 基础脚本文件 -->
<script src="<%= buildAssetPath('/assets/pageforge.min.js') %>"></script>

<script>
    // 移动端侧边栏打开时显示遮罩
    document.querySelector('[onclick*="sidebar-menu"]').addEventListener('click', function () {
        document.getElementById('sidebar-overlay').classList.remove('hidden');
    });
</script>

<!-- 加载主题配置中的额外 JS 文件 -->
<% if (locals.siteData?.theme?.extraJs && Array.isArray(locals.siteData?.theme?.extraJs)) { %>
    <% locals.siteData.theme.extraJs.forEach(js_path => { %>
        <script src="<%= buildAssetPath(js_path) %>"></script>
    <% }); %>
<% } %>

<!-- 是否启用 Lucide 图标库 -->
<%- include('../includes/feature-lucide') %>
</html>